package com.dropdb.demo.controller.wx;

import com.dropdb.demo.bean.User;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.bean.vo.WxGoodsDetailVO;
import com.dropdb.demo.bean.vo.WxHomeIndexVO;
import com.dropdb.demo.service.WxHomeAndGoodsService;
import com.dropdb.demo.shiro.MallToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Alone
 * @version 1.0
 * @date 2020/6/30 14:54
 */
@RestController
@RequestMapping("wx")
public class WxHomeAndGoodsController {

    @Autowired
    WxHomeAndGoodsService service;

    @RequestMapping("home/index")
    public BaseRespVO homeIndex() {
        WxHomeIndexVO vo = service.queryHomePage();
        return BaseRespVO.ok(vo);
    }

    @RequestMapping("goods/count")
    public BaseRespVO goodsCount() {
        Integer count = service.countTotalGoods();
        HashMap<String, Integer> map = new HashMap<>();
        map.put("goodsCount", count);
        return BaseRespVO.ok(map);
    }

    @RequestMapping("goods/list")
    public BaseRespVO goodsList(Integer categoryId,Integer page,Integer size,String sort,String order,String keyword,Integer brandId,Boolean isHot,Boolean isNew){
        Map<String,Object> vo=service.goodsList(categoryId,page,size,sort,order,keyword,brandId,isHot,isNew);
        return BaseRespVO.ok(vo);
    }

    @RequestMapping("goods/category")
    public BaseRespVO goodsCategory(Integer id) {
        Map<String, Object> vo = service.goodsCategory(id);
        return BaseRespVO.ok(vo);
    }

    /*@RequestMapping("cart/goodscount")
    public BaseRespVO cartCount() {
        return BaseRespVO.ok(11);
    }*/

    @RequestMapping("goods/detail")
    public BaseRespVO goodsDetail(Integer id) {
        WxGoodsDetailVO vo = service.goodsDetail(id);
        return BaseRespVO.ok(vo);
    }

    @RequestMapping("goods/related")
    public BaseRespVO goodsRelated(Integer id) {
        Map<String, Object> vo = service.goodsRelated(id);
        return BaseRespVO.ok(vo);
    }

    @RequestMapping("auth/login")
    public BaseRespVO authLogin(@RequestBody Map<String,String> bo) {
        MallToken mallToken = new MallToken(bo.get("username"), bo.get("password"), "wx");
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(mallToken);
        } catch (AuthenticationException e) {
            //e.printStackTrace();
            return BaseRespVO.error(402,"参数值不对");
        }

        /*HashMap<String, Object> map = new HashMap<>();
        map.put("token", "s996yqs204ej3g10qzbf6qd8mel6nru0");
        map.put("tokenExpire", new Date(System.currentTimeMillis()));
        HashMap userInfo = new HashMap<String, String>();
        userInfo.put("avatarUrl", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80");
        userInfo.put("nickName", "test1");
        map.put("userInfo", userInfo);*/
        /*User primaryPrincipal = (User) subject.getPreviousPrincipals().getPrimaryPrincipal();
        System.out.println(primaryPrincipal);*/
        Map<String,Object> vo=service.queryUser(bo.get("username"));
        return BaseRespVO.ok(vo);
    }

    @RequestMapping("auth/logout")
    public BaseRespVO authLogout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVO.ok();
    }

    @RequestMapping("user/index")
    public BaseRespVO userIndex() {
        /*Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        System.out.println(principal.getId()+"  "+principal.getNickname());*/
        Map<String, Map<String, Integer>> vo = service.userIndex();
        return BaseRespVO.ok(vo);
    }
}
